package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.mapper.EmployeeMapper;

import com.gxa.pojo.*;
import com.gxa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;





//   页码查询
    @Override
    public JsonResult queryEmployeeListPage(Integer page, Integer limit) {
        JsonResult r=new JsonResult();
        Integer parma1=(page-1)*limit;
        Integer parma2=limit;
        Integer count=employeeMapper.findCount();
        List<Employee> employeeList = employeeMapper.queryEmployeeListPage(parma1,parma2);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setCount(count);
        r.setData(employeeList);
        return r;
    }





//    模糊查询
    @Override
    public JsonResult queryEmployee(Integer page ,Integer pageSize ,String search) {
        //对查询页面参数计算
        Integer param1 = (page-1)*pageSize;
        Integer param2 = pageSize;
        Integer count=page*pageSize;
        JsonResult r=new JsonResult();
        //调用mapper方法，返回查询结果list
        List<Employee> list=employeeMapper.queryEmployeeList(param1,param2,search);
        //将list和count条数塞入r,响应成功返回
        r.setCode(200);
        r.setCount(count);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }





//   权限查询
    @Override
    public R queryAnthority(Role role) {
        R r =new R();
        r.setCode(200);
        r.setMsg("查询成功");
        List<Anthority> anthorityList = employeeMapper.queryAnthority(role);
        r.setData(anthorityList);
        return r;
    }





//    添加员工
    @Override
    public R addEmployee(Employee employee) {
        employeeMapper.addEmployee(employee);
        R r =new R();
        r.setCode(200);
        r.setMsg("添加成功");
        return  r;
    }





//    添加部门
    @Override
    public R addDept(Dept dept) {
        Integer result= employeeMapper.addDept(dept);
        R r =new R();
        r.setCode(200);
        r.setMsg("添加成功");
        r.setData(result);
        return  r;
    }




//    修改部门
    @Override
    public R updateDept(Dept dept) {
        R r =new R();
        r.setCode(200);
        r.setMsg("修改成功");
        int result= employeeMapper.updateDept(dept);
        r.setData(result);
        return r;
    }




//    修改权限
    @Override
    public R updateAnthority( Role_Anthority role_anthority) {
        R r =new R();
        String role_name=role_anthority.getRole_name();
        Integer role_id = employeeMapper.queryRoleId(role_name);
        String[] anthority_name= role_anthority.getAnthority_name();
        employeeMapper.delRoleAnthority(role_id);
        for (int x = 0;x<anthority_name.length;x++) {
            Integer ID=employeeMapper.queryAnthorityId(anthority_name[x]);
            employeeMapper.addRoleAnthority(role_id,ID);
        }
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }





//    查询角色
    @Override
    public JsonResult queryRole(Integer page , Integer pageSize) {
        Integer param1 = (page-1)*pageSize;
        Integer param2 = pageSize;
        Integer count=page*pageSize;
        JsonResult r=new JsonResult();
        List<Role> roleList= employeeMapper.queryRole(param1,param2);
        r.setMsg("查询成功");
        r.setCount(count);
        r.setData(roleList);
        return r;
    }




//    查询部门
    @Override
    public JsonResult queryDept(Integer page, Integer pageSize) {
        Integer param1 = (page-1)*pageSize;
        Integer param2 = pageSize;
        Integer count=employeeMapper.deptCount();
        JsonResult r=new JsonResult();
        List<Dept> roleList= employeeMapper.queryDept(param1,param2);
        r.setMsg("查询成功");
        r.setCount(count);
        r.setData(roleList);
        return r;
    }



//   新增角色，创建权限
    @Override
    public R addRole_Anthority(Role_Anthority role_anthority) {
        R r =new R();
        String role_name=role_anthority.getRole_name();
        String[] anthority_name=role_anthority.getAnthority_name();
        employeeMapper.addRole(role_name);//调用方法,添加角色
        Integer role_id= employeeMapper.queryRoleId(role_name);
        for (int x = 0;x<anthority_name.length;x++) {//循环查询权限id
            Integer ID=employeeMapper.queryAnthorityId(anthority_name[x]);
            employeeMapper.addRoleAnthority(role_id,ID);
        }
        r.setCode(200);
        r.setMsg("添加成功");
        return r;

    }






//    高级搜索
    @Override
    public JsonResult advancedSearch(Integer page, Integer pageSize, AdvancedSearch advancedSearch) {
        String employee_name=advancedSearch.getEmployee().getEmployee_name();
        Integer employee_sex=advancedSearch.getEmployee().getEmployee_sex();
        String employee_tel=advancedSearch.getEmployee().getEmployee_tel();
        Date employee_birthday=advancedSearch.getEmployee().getEmployee_birthday();
        Integer dept_id =advancedSearch.getEmployee().getDept_id();
        Integer state =advancedSearch.getEmployee().getEmployee_state();
        JsonResult r = new JsonResult();
        List<Employee> employeeList1 = employeeMapper.advancedSearch(page,pageSize,advancedSearch);
        r.setCode(200);
        r.setMsg("修改成功");
        r.setData(employeeList1);
        return r;
    }



//    修改员工信息
    @Override
    public JsonResult updateEmployee(Employee employee) {
        JsonResult r = new JsonResult();
        employeeMapper.updateEmployee(employee);
        r.setCode(200);
        Integer count=employeeMapper.findCount();
        r.setMsg("修改成功");
        r.setCount(count);
        return r;
    }




    //删除部门
    @Override
    public JsonResult delDept(Dept dept) {
        employeeMapper.delDept(dept);
        Integer count = employeeMapper.deptCount();
        JsonResult r =new JsonResult();
        r.setCode(200);
        r.setMsg("删除成功");
        r.setCount(count);
        return  r;
    }



//    删除员工
    @Override
    public JsonResult delEmployee(Employee employee) {
        employeeMapper.delEmployee(employee);
        Integer count=employeeMapper.findCount();
        JsonResult r =new JsonResult();
        r.setCode(200);
        r.setMsg("删除成功");
        r.setCount(count);
        return r;
    }


//    删除角色
    @Override
    public JsonResult delRole(Role role) {
        employeeMapper.delRole(role);
        Integer count=employeeMapper.roleCount();
        JsonResult r =new JsonResult();
        r.setCode(200);
        r.setMsg("删除成功");
        r.setCount(count);
        return r;
    }

//    查询所有员工
    @Override
    public JsonResult queryEmpList() {
        Integer count = employeeMapper.findCount();
        List<Employee> employeeList = employeeMapper.queryEmpList();
        JsonResult r=new JsonResult();
        r.setCount(count);
        r.setData(employeeList);
        return r;
    }


//    查询部门(不分页)
    @Override
    public JsonResult queryDept1() {
        JsonResult r=new JsonResult();
        List<Dept> deptList=employeeMapper.queryDept1();
        r.setCode(200);
        r.setData(deptList);
        return r;
    }

//    查询角色不分页
    @Override
    public JsonResult queryRole1() {
        JsonResult r=new JsonResult();
        List<Role> roleList=employeeMapper.queryRole1();
        r.setCode(200);
        r.setData(roleList);
        return r;
    }


}
